package com.aiyige.page.my.localVideo;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.math.BigDecimal;

/**
 * 缓存工具
 * 
 * @author dance
 * 
 */
public class CacheUtils {

	
	/**
	   * 格式化缓存大小
	   * 
	   * @param size
	   * @return
	   */
	public static String getFormatSize(int scale, double size) {
	    double kiloByte = size / 1024;
	    if (kiloByte < 1) {
	      return scale == 2 ? "0.00MB" : "0.0MB";
	    }

	    double megaByte = kiloByte / 1024;
	    if (megaByte < 1) {
	      BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
	      return result1.setScale(scale, BigDecimal.ROUND_HALF_UP)
	          .toPlainString() + "KB";
	    }

	    double gigaByte = megaByte / 1024;
	    if (gigaByte < 1) {
	      BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
	      return result2.setScale(scale, BigDecimal.ROUND_HALF_UP)
	          .toPlainString() + "MB";
	    }

	    double teraBytes = gigaByte / 1024;
	    if (teraBytes < 1) {
	      BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
	      return result3.setScale(scale, BigDecimal.ROUND_HALF_UP)
	          .toPlainString() + "GB";
	    }
	    BigDecimal result4 = new BigDecimal(teraBytes);
	    return result4.setScale(scale, BigDecimal.ROUND_HALF_UP).toPlainString()
	        + "TB";
	  }
	  
	/**
	 * 清理内部缓存(/data/data/com.xxx.xxx/cache)
	 */
	public static void cleanInternalCache(Context context) {
		deleteFilesByDirectory(context.getCacheDir());
	}

	/**
	 * �清理数据库(/data/data/com.xxx.xxx/databases)
	 * 
	 */
	public static void cleanDatabases(Context context) {
		deleteFilesByDirectory(new File("/data/data/"
				+ context.getPackageName() + "/databases"));
	}

	/**
	 * 清理SharedPreference(/data/data/com.xxx.xxx/shared_prefs)
	 */
	public static void cleanSharedPreference(Context context) {
		deleteFilesByDirectory(new File("/data/data/"
				+ context.getPackageName() + "/shared_prefs"));
	}

	/**
	 * 清理外部缓存(/mnt/sdcard/android/data/com.xxx.xxx/cache)
	 */
	public static void cleanExternalCache(Context context) {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			deleteFilesByDirectory(context.getExternalCacheDir());
		}
	}

	/**
	 * 清理缓存文件
	 * 
	 * */
	public static void cleanCustomCache(String filePath) {
		deleteFilesByDirectory(new File(filePath));
	}

	/**
	 * 删除文件夹中的文件
	 */
	private static void deleteFilesByDirectory(File directory) {
		if (directory != null && directory.exists() && directory.isDirectory()) {
			for (File item : directory.listFiles()) {
				item.delete();
			}
		}
	}

	/**
	 * 获取文件夹图片
	 */
	public static long getFolderSize(File file){
		long size = 0;
		try {
			File[] fileList = file.listFiles();
			for (int i = 0; i < fileList.length; i++) {
				if (fileList[i].isDirectory()) {
					size = size + getFolderSize(fileList[i]);
				} else {
					size = size + fileList[i].length();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return size;
	}
	
}
